package com.hxh.wechat.adpter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import coil.load
import com.billy.android.swipe.SmartSwipe
import com.billy.android.swipe.SmartSwipeWrapper
import com.billy.android.swipe.SwipeConsumer
import com.billy.android.swipe.consumer.SlidingConsumer
import com.billy.android.swipe.listener.SimpleSwipeListener
import com.hxh.wechat.R
import com.hxh.wechat.activity.ActivityChat
import com.hxh.wechat.activity.ChatPageActivity
import com.hxh.wechat.model.MsgType
import com.hxh.wechat.model.Star

class StarAdapter(private val starList: List<Star>) :
    RecyclerView.Adapter<StarAdapter.ViewHolder>() {

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val fruitImage: ImageView = view.findViewById(R.id.starImg)
        val fruitName: TextView = view.findViewById(R.id.starName)
        val starContent: TextView = view.findViewById(R.id.starContent)
        val time: TextView = view.findViewById(R.id.time)
        val textViewBadge: TextView = view.findViewById(R.id.textViewBadge)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        var inflater = LayoutInflater.from(parent.context)
        val view = inflater
            .inflate(R.layout.listview, parent, false)

        var swipMenu = inflater.inflate(
            R.layout.chat_sideslip_menu,
            parent,
            false
        )
        var slidingConsumer = SlidingConsumer()

        //侧滑删除
        var swipeConsumer = SmartSwipe.wrap(view)
            .addConsumer(slidingConsumer)
            .setHorizontalDrawerView(swipMenu)
            .enableRight() //启用左右两侧侧滑

        var viewHolder = ViewHolder(swipeConsumer.wrapper)
        //绑定事件
        view.setOnClickListener {
            val position = viewHolder.getAdapterPosition();
            val star = starList[position]

            if (star.type == MsgType.IMAGE.type || star.type == MsgType.TEXT.type) {
                ChatPageActivity.goChat(
                    view.context,
                    ContactAdapter.ContactItem(
                        star.userId,
                        star.userImg,
                        star.content,
                        star.userName
                    )
                )
            } else {
                println("第三方界面")
            }
        }
        slidingConsumer.addListener(object : SimpleSwipeListener() {
            override fun onSwipeOpened(
                wrapper: SmartSwipeWrapper?,
                consumer: SwipeConsumer?,
                direction: Int
            ) {
                val position = viewHolder.getAdapterPosition();
                val star = starList[position]
                //点击跳转
                /*if (star.type == MsgType.IMAGE.type || star.type == MsgType.TEXT.type) {
                    // 给列表项添加点击事件处理逻辑
                    swipMenu.findViewById<TextView>(R.id.chat).setOnClickListener {
                        ChatPageActivity.goChat(
                            view.context,
                            ContactAdapter.ContactItem(
                                star.userId,
                                star.userImg,
                                star.content,
                                star.userName
                            )
                        )
                    }
                } else {
                    swipMenu.findViewById<TextView>(R.id.chat).setOnClickListener {
                        println("第三方界面")
                    }
                }*/

                //置顶
                swipMenu.findViewById<TextView>(R.id.top).setOnClickListener { }
                //删除
                swipMenu.findViewById<TextView>(R.id.delete).setOnClickListener {
                    ActivityChat.starList.remove(star)
                    ActivityChat.notifyDataSetChanged()
                }
            }
        })
        return viewHolder
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val star = starList[position]
        holder.fruitImage.load(star.userImg)
        holder.fruitName.text = star.userName;
        if (star.type == MsgType.TEXT.type) {
            if (star.content.length > 20) {
                holder.starContent.text = star.content.subSequence(0, 20).toString() + ".."
            } else {
                holder.starContent.text = star.content
            }
//                EmoJiUtils.parseEmoJi(1, holder.itemView.context, star.content)
        } else if (star.type == MsgType.IMAGE.type) {
            holder.starContent.text = "[图片]"
        } else if (star.type == MsgType.APP.type) {
            holder.starContent.text = star.content
        }
        holder.time.text = star.time.toLocaleString()
        holder.textViewBadge.text = star.size.toString()
        if (star.size == 0) {
            holder.textViewBadge.visibility = View.GONE;
        } else {
            holder.textViewBadge.visibility = View.VISIBLE;
        }
    }


    override fun getItemCount() = starList.size
}

